package com.epfly.promo.activity.impl.discount;

import com.epfly.iface.promo.activity.enums.ActivityRuleDinnerTypeEnum;
import com.epfly.promo.activity.dao.ActivityRuleMapper;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.ActivityRuleEntity;
import com.epfly.promo.activity.feign.DataRestaurantFeignService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;

public abstract class AbstractDiscountFunction implements ActivityDiscountFunction {

    protected Gson gson = new GsonBuilder().create();

    @Resource
    private ActivityRuleMapper activityRuleMapper;

    @Resource
    private DataRestaurantFeignService dataRestaurantFeignService;

    @Override
    public DiscountFun apply(ActivityWrapper activityWrapper) {
        ActivityEntity activityEntity = activityWrapper.getActivityEntity();

        Byte dinnerType = dataRestaurantFeignService.getMealType(activityWrapper.getMerchantId(), activityWrapper.getStoreId(), activityWrapper.getOrderTime()).byteValue();

        ActivityRuleEntity ruleEntity = activityRuleMapper.findByActivityIdAndDinnerTypeList(activityEntity.getId(), Arrays.asList(dinnerType, ActivityRuleDinnerTypeEnum.UN_LIMIT.getKey()));
        BigDecimal discountAmount = doApply(activityWrapper, ruleEntity);

        DiscountFun discountFun = new DiscountFun();
        discountFun.setActivityId(activityEntity.getId());
        discountFun.setDiscountAmount(discountAmount);
        return discountFun;
    }

    public abstract BigDecimal doApply(ActivityWrapper activityWrapper, ActivityRuleEntity ruleEntity);
}
